package uit.edu.vn.Activity;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TabHost;

public class ContactRestoreActivity extends TabActivity
{
    private String username ="";
    private String password ="";
    private static final String STRING_NAME_USERSETTING = "USER REFERENCES";
    private SharedPreferences ShareRef = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tabview);
        username = getSetting("username");
        password = getSetting("password");
	      //kiem tra neu username va password rong thi ket thuc activity hien tai va goi activity login
	      if(username.equals("") && password .equals("")){
	          Intent intent = new Intent(ContactRestoreActivity.this, LoginActivity.class);
	          startActivity(intent);
	          this.finish();            	
	      }
		   Resources res = getResources(); 
	        TabHost tabHost= getTabHost();
	        TabHost.TabSpec spec;
	        // Tạo intent và gọi các class tiep theo
	        Intent i;
	        //goi ContactRestoreSDCardActivity.class
	        i= new Intent().setClass(this, ContactRestoreSDCardActivity.class);
	        spec= tabHost.newTabSpec("").setIndicator("Thẻ Nhớ",res.getDrawable(R.drawable.tab_restore_sdcard_seletor)).setContent(i);
	        tabHost.addTab(spec);
	        // goi ContactRestoreServerActivity.class
	        i= new Intent().setClass(this, ContactRestoreServerActivity.class);
	        spec= tabHost.newTabSpec("").setIndicator("Máy Chủ",res.getDrawable(R.drawable.tab_restore_selector)).setContent(i);
	        tabHost.addTab(spec);
	        tabHost.setCurrentTab(0);        
	}
    public void saveSetting(String key, String value) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        Editor editor = ShareRef.edit();
        editor.putString(key, value);
        editor.commit();
    }
	public String getSetting(String key) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        return ShareRef.getString(key, "");
	}
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
    	{
    		showConfirmDialog(getResources().getString(R.string.dialog_thongbao),getResources().getString(R.string.dialog_message_thoat) , new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					logOut();
					finish();
				}
			});
    	}
    	return super.onKeyDown(keyCode, event);
    }
    public AlertDialog showConfirmDialog(String title, String msg, OnClickListener yesClick) 
	{
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(R.drawable.ic_error);
			if(title!=null) builder.setTitle(title);
			if(msg!=null) builder.setMessage(msg);
			return builder.setPositiveButton(getResources().getString(R.string.txt_Yes), yesClick).setNegativeButton(getResources().getString(R.string.txt_No), new OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			}).show();
	}
    
    public void logOut() {
        saveSetting("username", "");
        saveSetting("password", "");
        saveSetting("userid", "");
    }
}
